package com.wangshupo.app.geekpark.spider;

import com.alibaba.fastjson.JSON;
import com.wangshupo.app.geekpark.model.Author;
import com.wangshupo.app.geekpark.model.News;
import com.wangshupo.app.geekpark.service.AuthorService;
import com.wangshupo.app.geekpark.service.NewsService;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;

import javax.annotation.PostConstruct;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Component
public class NewsSpider {
    private static final Logger LOG = LoggerFactory.getLogger(NewsSpider.class);

    private static final String news_url = "https://mainssl.geekpark.net/api/v2?page=";

    private static final int max_page = 2;

    // okHttpClient 实例
    private static final OkHttpClient okHttpClient = new OkHttpClient();

    @Autowired
    private NewsService newsService;

    @Autowired
    private AuthorService authorService;

    @PostConstruct
    public void start() {
        for (int i = 1; i <= max_page; i++) {
            String url = news_url + i;
            String content = getContent(url);
            Map sourceData = JSON.parseObject(content, Map.class);

            parseHomepageNews(sourceData);

            parseTopNews(sourceData);

            LOG.info("parse page " + i + " news spider complete...");
        }

        LOG.info("news spider complete...");
    }

    private void parseTopNews(Map sourceData) {
        Map slider = (Map)sourceData.get("slider");
        List<Map> posts = (List<Map>)slider.get("posts");
        for (Map postData : posts) {
            saveNews(postData, "top");
        }

    }

    private void parseHomepageNews(Map sourceData) {
        List<Map> homepagePosts = (List<Map>)sourceData.get("homepage_posts");

        for (Map hp : homepagePosts) {
            Map postData = (Map)hp.get("post");
            saveNews(postData, null);
        }
    }

    private void saveNews(Map postData, String type) {
        News news = new News();
        news.setTitle((String)postData.get("title"));
        news.setDesc((String)postData.get("abstract"));
        news.setCover((String)postData.get("cover_url"));
        news.setId(String.valueOf(postData.get("id")));

        Map column = (Map)postData.get("column");

        if (column != null) {
            type = (String)column.get("title");
        }

        news.setType(type);

        List<Map> authors = (List<Map>)postData.get("authors");
        List<String> authorIds = saveAuthors(authors);
        news.setAuthorIds(authorIds);

        News oldNews = newsService.getNewsById(news.getId());

        if (oldNews == null) {
            newsService.addNews(news);
        } else {
            newsService.modify(news);
        }
    }

    private List<String> saveAuthors(List<Map> authors) {
        List<String> authorIds = new ArrayList<>();

        if (CollectionUtils.isEmpty(authors)) {
            return authorIds;
        }

        for (Map authorData : authors) {
            Author author = new Author();
            author.setId((String)authorData.get("id"));
            author.setName((String)authorData.get("realname"));
            author.setNick((String)authorData.get("nickname"));
            author.setAvatar((String)authorData.get("avatar_url"));

            authorIds.add(author.getId());

            Author oldAuthor = authorService.getAuthorById(author.getId());

            if (oldAuthor == null) {
                authorService.addAuthor(author);
            } else {
                authorService.modify(author);
            }
        }

        return authorIds;
    }

    /**
     * 根据输入的url，读取页面内容并返回
     */
    private String getContent(String url) {
        // 定义一个request
        Request request = new Request.Builder().url(url).addHeader("User-Agent",
                                                                   "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/78.0.3904.108 Safari/537.36")
            .addHeader("Referer", "https://www.geekpark.net/").addHeader("Host", "mainssl.geekpark.net").build();
        // 返回结果字符串
        String result = null;
        try {
            // 执行请求
            Response response = okHttpClient.newCall(request).execute();
            result = response.body().string();
        } catch (IOException e) {
            System.out.println("request " + url + " error . ");
            e.printStackTrace();
        }
        return result;
    }

}
